﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.IO;
using System.Text;
using System.Data;
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            System.Net.WebRequest request = System.Net.WebRequest.Create("http://192.168.43.201/BHXHHY/Home/getTradeContribs?portfolioId=1&nodedates=" + TextBox1.Text);

            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json; charset=utf-8";
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            //get response-stream, and use a streamReader to read the content
            using (Stream s = request.GetResponse().GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(s))
                {
                    //Test test = (Test)json_serializer.DeserializeObject(); 
                    //decode jsonData with javascript serializer

                    //JObject jObject = JObject.Parse(sr.ReadToEnd());
                    //JToken jUser = jObject["Name"] + "<br/>" + jObject["Expiry"] + "<br/>" + jObject["Price"] + "<br/>" + jObject["Sizes"];
                    //Product product = new Product(sr.ReadToEnd());
                    string[] stringSeparators = new string[] { "},{" };
                    string[] words = sr.ReadToEnd().ToString().TrimStart('[').TrimEnd(']').Split(stringSeparators, StringSplitOptions.None);

                    StringBuilder val = new StringBuilder();
                    StringBuilder key = new StringBuilder();
                    string value;

                    System.Data.DataTable myTable = new DataTable("Dic");
                    System.Data.DataColumn column;
                    System.Data.DataRow row;

                    //create 3 columns
                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.String");
                    column.ColumnName = "Key";
                    column.ReadOnly = false;
                    // Add the Column to the DataColumnCollection.
                    myTable.Columns.Add(column);

                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.String");
                    column.ColumnName = "Value";
                    column.ReadOnly = false;
                    // Add the Column to the DataColumnCollection.
                    myTable.Columns.Add(column);


                    foreach (string word in words)
                    {
                        row = myTable.NewRow();
                        value = word.TrimStart('{').TrimEnd('}');
                        Dic dic = new Dic("{" + value + "}");
                        val.Append(" " + dic.Value);
                        key.Append(" " + dic.Key);

                        row["Key"] = dic.Key;
                        row["Value"] = dic.Value;
                        myTable.Rows.Add(row);
                    }
                    
                    GridView1.DataSource = myTable;
                    GridView1.DataBind();
                    
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create("http://192.168.43.201/BHXHHY/Home/insert?key=" + txtKey.Text + "&value=" + txtVal.Text);
            request.Method = "POST";
            request.ContentType = "application/json; charset=utf-8";
            //get response-stream, and use a streamReader to read the content
            Stream s = request.GetResponse().GetResponseStream();

        }
    }
}